package com.tadpole.music.view.fragment.question;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.luck.picture.lib.permissions.RxPermissions;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.question.SimulationPracticeAdapter;
import com.tadpole.music.bean.question.QuestionClassBean;
import com.tadpole.music.bean.question.TrainingQuestionBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.QuestionClassIView;
import com.tadpole.music.iView.question.TrainingQuestionIView;
import com.tadpole.music.presenter.question.QuestionClassPresenter;
import com.tadpole.music.presenter.question.TrainingQuestionPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.me.MemberCenterActivity;
import com.tadpole.music.view.activity.question.RealTrainActivity;
import com.tadpole.music.view.activity.question.SimulationExerciseActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 模拟练习
 * Created By 孙岩
 * on 2020/9/11
 */
public class SimulationPracticeFragment extends BaseFragment implements QuestionClassIView, TrainingQuestionIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM4 = "param4";
    //院校id
    private String mParam1;
    //等级id
    private String mParam2;
    //院校名称
    private String mParam4;
    //分类id
    private String question_class_id;
    private RecyclerView recyclerView;
    private SimulationPracticeAdapter simulationPracticeAdapter;
    //试唱
    private ImageView ivShiChang;
    //模唱
    private ImageView ivMoChang;
    //实战训练
    private TextView tvShizhan;
    //获取分类
    private QuestionClassPresenter questionClassPresenter;
    //获取题目
    private TrainingQuestionPresenter trainingQuestionPresenter;

    public static SimulationPracticeFragment newInstance(String param1, String param2, String param4) {
        SimulationPracticeFragment fragment = new SimulationPracticeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM4, param4);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_simulation_practice;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            mParam4 = getArguments().getString(ARG_PARAM4);
        }
    }

    @Override
    protected void initView(View mRootView) {
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        ivShiChang = mRootView.findViewById(R.id.ivShiChang);
        ivMoChang = mRootView.findViewById(R.id.ivMoChang);
        tvShizhan = mRootView.findViewById(R.id.tvShizhan);

        //获取分类
        questionClassPresenter = new QuestionClassPresenter();
        questionClassPresenter.attachView(this);
        questionClassPresenter.getQuestion_class(mParam1, mParam2);

        //获取题目
        trainingQuestionPresenter = new TrainingQuestionPresenter();
        trainingQuestionPresenter.attachView(this);


        tvShizhan.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    Intent intent = new Intent(getActivity(), RealTrainActivity.class);
                    intent.putExtra("id", Integer.valueOf(mParam1));
                    intent.putExtra("name", mParam4);
                    startActivity(intent);
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void showQuestionClass(final List<QuestionClassBean.DataBean> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 4);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        simulationPracticeAdapter = new SimulationPracticeAdapter(getActivity(), list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(simulationPracticeAdapter);
        recyclerView.setNestedScrollingEnabled(false);

        simulationPracticeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (Mutils.isNetworkAvailable()) {

                    question_class_id = String.valueOf(list.get(position).getId());

                    andPermission(String.valueOf(list.get(position).getId()));
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void show401() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(getActivity()).putString(Constant.TOKEN, "");
    }

    //权限
    @SuppressLint("CheckResult")
    private void andPermission(final String question_class_id) {
        RxPermissions rxPermission = new RxPermissions(getActivity());
        rxPermission
                .request(
                        Manifest.permission.MODIFY_AUDIO_SETTINGS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //请求权限成功
                if (aBoolean) {
                    trainingQuestionPresenter.getTrainingQuestion(1, mParam1, mParam2, question_class_id, 0);
                } else {//失败
                    String msg = "请点击【去授权】打开相应权限！";
                    showAlert(msg);
                }
            }
        });
    }

    //授权
    public void showAlert(String msg) {
        new AlertDialog.Builder(getActivity()).setMessage(msg).
                setPositiveButton("去授权",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setCancelable(true).show();
    }

    @Override
    public void goToPay() {
        Intent intent = new Intent(getActivity(), MemberCenterActivity.class);
        intent.putExtra("flag","1");
        intent.putExtra("college_id",mParam1);
        intent.putExtra("rank_id",mParam2);
        startActivity(intent);
    }

    @Override
    public void showQuestion(TrainingQuestionBean.DataBeanX bean) {
        Intent intent = new Intent(getActivity(), SimulationExerciseActivity.class);
        intent.putExtra("college_id", mParam1);
        intent.putExtra("question_rank_id", mParam2);
        intent.putExtra("question_class_id", question_class_id);
        startActivity(intent);
    }

    @Override
    public void go401() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(getActivity()).putString(Constant.TOKEN, "");
    }
}
